<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/12/19
 * Time: 10:52
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Consumer extends Base
{
    /**
     * 首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //角色
            if(isset($params['user_type']) && $params['user_type'] != ''){
                $where['user_type'] = $params['user_type'];
            }
            //店铺名称
            if(isset($params['user_shopname']) && $params['user_shopname'] != ''){
                $where['user_shopname'] = ['like', "%" . $params['user_shopname'] . "%"];
            }
            //用户名或手机
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['user_username|user_phone'] = ['like', "%" . $params['key'] . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')->where($where)->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->field('user_id, user_shopname, user_username, user_phone, user_status, user_ssq_str, 
                user_type, user_money')
                ->page($Nowpage,$limits)
                ->order('user_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 修改用户状态
     * @return \think\response\Json
     */
    public function memberState(){
        $num = input('num');
        $user_id = input('id');
        $user_status = Db::table('up_u_user')->where('user_id', $user_id)->value('user_status');
        if($user_status == 0 || $user_status == 2){
            return json(['code' => 100, 'msg' => '用户未审核通过，不允许操作']);
        }
        if($num == 3){//正常->冻结
            $msg = '冻结成功';
        }else{
            $msg = '启用成功';
        }
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 用户审核
     * @return mixed
     */
    public function audit(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            $update['user_status'] = $params['user_status'];//审核结果
            
            //如果的是运营，且审核通过，需判断该用户所在城市是否已有同品牌运营
            $info = Db::table('up_u_user')->where('user_id', $params['user_id'])->find();
            if($info['user_type'] == 2 && $params['user_status'] == 1){
                $update['user_can_yuyue'] = $params['user_can_yuyue'];//预约资格
                //申请代理
                $isHave = Db::table('up_u_user')
                    ->where('user_shi', $info['user_shi'])
                    ->where('user_type', 2)
                    ->where('user_pinpai_id', $info['user_pinpai_id'])
                    ->where(function ($query){
                        $query->where('user_status', 1)->whereOr('user_status', 3);
                    })
                    ->find();
                if($isHave){
                    return json(['code' => 100, 'msg' => '该城市已有同品牌运营，不能审核通过']);
                }
                //预约资格
                if($params['user_can_yuyue'] == 1){
                    $isCan = Db::table('up_u_user')
                        ->where('user_shi', $info['user_shi'])
                        ->where('user_type', 2)
                        ->where('user_can_yuyue', 1)
                        ->where(function ($query){
                            $query->where('user_status', 1)->whereOr('user_status', 3);
                        })
                        ->find();
                    if($isCan){
                        return json(['code' => 100, 'msg' => '该城市已有预约资格的代理，请重新审核']);
                    }
                }
            }
           
            $res = Db::table('up_u_user')
                ->where('user_id', $params['user_id'])
                ->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '审核成功']);
            }else{
                return json(['code' => 100, 'msg' => '审核失败']);
            }
        }
        $user_id = input('id');
        $userInfo = Db::table('up_u_user')
            ->join('up_pinpai', 'p_id = user_pinpai_id', 'left')
            ->field([
                'user_image',
                'user_username',
                'user_phone',
                'user_idcard',
                'user_idcard_img',
                'user_zhizhao',
                'user_shopname',
                'user_type',
                'user_ssq_str',
                'user_address',
                'user_zizhi',
                'user_time',
                'user_type',
                'p_name'
            ])
            ->where('user_id', $user_id)
            ->find();
        //身份证照片
        if(!empty($userInfo['user_idcard_img'])) {
            $userInfo['user_idcard_img'] = explode(',', $userInfo['user_idcard_img']);
        } else {
            $userInfo['user_idcard_img'] = [];
        }
        //营业执照
        if(!empty($userInfo['user_zhizhao'])) {
            $userInfo['user_zhizhao'] = explode(',', $userInfo['user_zhizhao']);
        } else {
            $userInfo['user_zhizhao'] = [];
        }
        //资质
        if(!empty($userInfo['user_zizhi'])) {
            $userInfo['user_zizhi'] = explode(',', $userInfo['user_zizhi']);
        } else {
            $userInfo['user_zizhi'] = [];
        }
        $this->assign([
            'user_id' => $user_id,
            'info' => $userInfo
        ]);
        return $this->fetch();
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function detail(){
        $user_id = input('id');
        $userInfo = Db::table('up_u_user')
            ->join('up_pinpai', 'p_id = user_pinpai_id', 'left')
            ->field([
                'user_image',
                'user_username',
                'user_phone',
                'user_idcard',
                'user_idcard_img',
                'user_zhizhao',
                'user_shopname',
                'user_type',
                'user_ssq_str',
                'user_address',
                'user_zizhi',
                'user_time',
                'user_money',
                'user_status',
                'p_name',
                'user_can_yuyue'
            ])
            ->where('user_id', $user_id)
            ->find();
        //身份证照片
        if(!empty($userInfo['user_idcard_img'])) {
            $userInfo['user_idcard_img'] = explode(',', $userInfo['user_idcard_img']);
        } else {
            $userInfo['user_idcard_img'] = [];
        }
        //营业执照
        if(!empty($userInfo['user_zhizhao'])) {
            $userInfo['user_zhizhao'] = explode(',', $userInfo['user_zhizhao']);
        } else {
            $userInfo['user_zhizhao'] = [];
        }
        //资质
        if(!empty($userInfo['user_zizhi'])) {
            $userInfo['user_zizhi'] = explode(',', $userInfo['user_zizhi']);
        } else {
            $userInfo['user_zizhi'] = [];
        }
        $this->assign([
            'user_id' => $user_id,
            'info' => $userInfo
        ]);
        return $this->fetch();
    }
    
    /**
     * 删除用户
     * @return \think\response\Json
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 添加用户余额
     * @return mixed|\think\response\Json
     */
    public function addBalance(){
        if(Request::instance()->isPost()){
            $money = input('money');
            $user_id = input('user_id');
            $res = Db::table('up_u_user')->where('user_id', $user_id)->setInc('user_money', $money);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $id = input('id');
        $this->assign('user_id', $id);
        return $this->fetch('consumer/addbalance');
    }
}